package com.ironman.original.generator;


import cn.hutool.core.util.StrUtil;
import com.ironman.event.topic.dami.WindTopic;
import org.noear.dami.Dami;
import org.noear.snack.ONode;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class WindOriginalDataGenerator extends OriginalDataGenerator {

    private String windSourceBoatCode;

    private String speed;

    private String direction;

    @PostConstruct
    public void init() {
        Dami.<String, Void>bus().listen(WindTopic.GW_WIND_TOPIC, payload -> {
            ONode node = ONode.loadStr(payload.getContent());
            String windSourceBoatCode = node.select("windSourceBoatCode").getString();
            String speed = node.select("speed").getString();
            String direction = node.select("direction").getString();
            updateWind(windSourceBoatCode, speed, direction);
        });
    }

    private synchronized void updateWind(String windSourceBoatCode, String speed, String direction) {
        this.windSourceBoatCode = windSourceBoatCode;
        this.speed = speed;
        this.direction = direction;
    }

    private synchronized String getWind() {
        return StrUtil.format("{},{},{}", windSourceBoatCode, speed, direction);
    }

    @Override
    public String dataType() {
        return "wind";
    }

    @Override
    public String generate() {
        return getWind();
    }
}
